package com.duapk.web.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpClientTest {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		new Thread(new Tst(),"Test").start();
	}

}

class Tst implements Runnable {

	@Override
	public void run() {
		URL url = null;
		HttpURLConnection httpCon = null;
		try {
			//url = new URL("http://114.119.55.158:9876/passport");
			//url = new URL("http://192.168.0.232:8080/appstore-1.0/msg");
			//url = new URL("http://115.174.23.226:9876/passport");
			url = new URL("http://127.0.0.1:8890/account");
			httpCon = (HttpURLConnection) url.openConnection();
			httpCon.setDoOutput(true);
			httpCon.setRequestMethod("POST");

//			PayMessageRequest req = new PayMessageRequest();
//			req.setMessageId(1000);
//			req.setNodeId("01234567890123456789");
//			req.setNodetype(0);
//			req.setVersion("2.0");
//			req.setTokenId("48932338046210316800370054247871");
//			req.setTransId("SP201105190000000031");
//			req.setFee(5);
//			req.setFeeID("100000100002");
//			req.setCPOrderID("100000100002abss");
			//			UserUpdateMessageRequest req = new UserUpdateMessageRequest();
			//			req.setMessageId(1000);
			//			req.setNodeId("01234567890123456789");
			//			req.setNodetype(200);
			//			req.setVersion("2.0");
			//			req.setTokenId("29449741089732044016615310657484");
			//			List<ParamSchema> pss = new ArrayList<ParamSchema>();
			//			ParamSchema ps = new ParamSchema();
			//			ps.setParamName("CompanyShortName");
			//			ps.setParamValue("千百夜");
			//			req.setParamNum(1);
			//			pss.add(ps);
			//			req.setParamSchemas(pss);
//			GetAccountMessageRequest req = new GetAccountMessageRequest();
//			req.setMessageId(1000);
//			req.setNodeId("01234567890123456789");
//			req.setNodetype(0);
//			req.setVersion("2.0");
//			req.setTokenId("73532595872940064615418919420228");
//			String message = JSONUtils.toJson(req);
			String postBody = "{\"Body\":{\"SMSCfgVersion\":\"1\",\"IMEI\":\"356693036674135\",\"IMSI\":\"460000230960584\",\"SMSC\":\"13800755500\"},\"CommandID\":1025,\"Version\":\"2.0\",\"MsgID\":50,\"RetCode\":0,\"NodeType\":0};";
			//String postBody = "";
//			message = postBody;
	//		System.out.println(message);
			OutputStream output = httpCon.getOutputStream();
			output.write(postBody.getBytes("UTF-8"));
			output.flush();
			output.close();
			int code = httpCon.getResponseCode();
			System.out.println("code   " + code);
			InputStream in = httpCon.getInputStream();
			byte[] readByte = new byte[1024];
			int readCount = in.read(readByte, 0, 1024);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while (readCount != -1) {
				baos.write(readByte, 0, readCount);
				readCount = in.read(readByte, 0, 1024);
			}
			String responseBody = new String(baos.toByteArray(), "UTF-8");
			baos.close();
			System.out.println(responseBody);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (httpCon != null)
				httpCon.disconnect();
		}
	}

}

